package com.tuananh.radnom.model.protocol.dhcp;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.HashMap;

import org.dhcp4java.DHCPConstants;
import org.dhcp4java.DHCPPacket;
import org.eclipse.persistence.oxm.annotations.XmlDiscriminatorValue;

import com.tuananh.radnom.model.discovery.DiscoverNetInt;
import com.tuananh.radnom.model.iputils.InetTools;
import com.tuananh.radnom.model.protocol.service.PassiveDiscover;

@XmlDiscriminatorValue("PassiveDhcp")
public class PassiveDhcp extends PassiveDiscover {

	public void run() {
		// TODO Auto-generated method stub
		System.out.println("PassiveDHCP started");
		try {
			DatagramSocket socket = new DatagramSocket(
					DHCPConstants.BOOTP_REQUEST_PORT);
			while (true) {
				DatagramPacket pac = new DatagramPacket(new byte[1500], 1500);
				DHCPPacket dhcp;
				byte byt = 12;
				socket.receive(pac);
				dhcp = DHCPPacket.getPacket(pac);
				
				if (!dhcp.getAddress().isAnyLocalAddress()) {
					DiscoverNetInt discovered = new DiscoverNetInt();
					discovered.setIp(dhcp.getAddress().getHostAddress());
					discovered.setNodeName(dhcp.getOptionAsString((byte) 12));
					discovered.setMac(InetTools.MACtoString(dhcp.getChaddr()));
					System.out.println(discovered.getMac());
					this.updateObservers(discovered);
				}
			}
		} catch (SocketException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public ArrayList<String> broadcastDiscover() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ArrayList<String> discoverNetInt() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public HashMap<String, String> discoverType(ArrayList<String> discoveredIPs) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public HashMap<String, String> discoverConnection(ArrayList<String> inputIPs) {
		// TODO Auto-generated method stub
		return null;
	}

}
